package com.syh.jn.scm.infrastructure.repository.impl.upstream.base;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.upstream.entity.base.UpBankAccount;
import com.syh.jn.scm.domain.upstream.repository.base.UpBankAccountRepository;
import com.syh.jn.scm.dto.upstream.base.UpBankAccountVo;
import com.syh.jn.scm.infrastructure.repository.model.base.BusinessEntityBankAccountModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

@Repository
public class UpBankAccountRepositoryImpl extends AbstractAggregateRepository<UpBankAccount, BusinessEntityBankAccountModel, Long> implements UpBankAccountRepository {

  @Override
  public Long add(UpBankAccount upBankAccount) {
    BusinessEntityBankAccountModel businessEntityBankAccountModel = new BusinessEntityBankAccountModel();
    ExtBeanUtils.copyProperties(upBankAccount, businessEntityBankAccountModel);
    businessEntityBankAccountModel.setTerminalType("UPSTREAM");
    return (Long) this.hibernateTemplate.save(businessEntityBankAccountModel);
  }

  @Override
  public int getCount(Wrapper wrapper) {
    wrapper.append(Restrictions.eq("terminalType", "UPSTREAM"));
    return super.getCount(wrapper);
  }

  /**
   * 内部方法，所有列表查询都调用本方法 改方法是Hibernate实现 注意增删改 业务必须调用该接口数据 不能用view后缀的数据
   *
   * @param wrapper
   * @param startRow
   * @param count
   * @return
   */
  @Override
  protected List<UpBankAccount> superFindListPage(Wrapper wrapper, Integer startRow, Integer count) {
    wrapper.append(Restrictions.eq("terminalType", "UPSTREAM"));
    return super.superFindListPage(wrapper, startRow, count);
  }

  /**
   * 内部方法，所有Sql执行的列表查询都调用本方法 改方法是 Mybatis 实现 注意增删改 业务不能调用该接口数据
   *
   * @param wrapper
   * @param startRow
   * @param count
   * @return
   */
  @Override
  protected <T> List<T> superFindListPageView(Wrapper wrapper, Integer startRow, Integer count) {
    wrapper.append(Restrictions.eq("terminalType", "UPSTREAM"));
    return super.superFindListPageView(wrapper, startRow, count);
  }

  @Override
  public int getCountView(Wrapper wrapper) {
    wrapper.append(Restrictions.eq("terminalType", "UPSTREAM"));
    return super.getCountView(wrapper);
  }

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    HashMap<String, String> params = new HashMap<>();
    params.put("id", "upBank.ID");
    params.put("status", "upBank.STATUS");
    params.put("accountBank", "upBank.ACCOUNT_BANK");
    params.put("accountBranchBank", "upBank.ACCOUNT_BRANCH_BANK");
    params.put("businessEntityId", "upBank.BUSINESS_ENTITY_ID");
    return super.getViewWrapperResolve(wrapper);
  }

  @Override
  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(SQLAliasGenerateUtil.aliasGenerate(BusinessEntityBankAccountModel.class, "upBank", null));
      FROM("T_BASE_BUSINESS_ENTITY_BANK_ACCOUNT as upBank");
    }};
  }

  @Override
  protected Class getViewObject() {
    return UpBankAccountVo.class;
  }
}
